﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevComponents.AdvTree;
using PlanningGIS.ManageUtility;
using PlanningGIS.ManageUtility.Modules;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Manager
{
    /// <summary>
    /// 模板树对象
    /// </summary>
    public partial class MXDTree : AdvTree
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MXDTree()
        {
            InitializeComponent();
            SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
                             System.Windows.Forms.ControlStyles.ResizeRedraw | System.Windows.Forms.ControlStyles.UserPaint, true);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="container"></param>
        public MXDTree(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
                 System.Windows.Forms.ControlStyles.ResizeRedraw | System.Windows.Forms.ControlStyles.UserPaint, true);
        }

        /// <summary>
        /// 模板信息是否改变
        /// </summary>
        private bool _IsChange = false;

        /// <summary>
        /// 模板信息是否改变
        /// </summary>
        public bool IsChange
        {
            get { return _IsChange; }
        }

        /// <summary>
        /// 重载资源选择事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnAfterCheck(AdvTreeCellEventArgs e)
        {
            _IsChange = true;
            base.OnAfterCheck(e);
        }

        /// <summary>
        /// 重置控件
        /// </summary>
        public void ResetControl()
        {
            _IsChange = false;
        }

        ///// <summary>
        ///// 所有的模板信息
        ///// </summary>
        //private List<MXDInfo> MXDs = null;
        
        /// <summary>
        /// 开始加载时绑定数据，并设定初始的选择状态
        /// </summary>
        /// <param name="remove">是否移除没有启用的模板</param>
        public void BindData(bool remove = false)
        {
            try
            {
                _IsChange = false;
                this.Nodes.Clear();
                List<MXDInfo> MXDs = ManageHelper.DBContext.GetAllEntry<MXDInfo>();
                if (MXDs == null || MXDs.Count == 0) return;
                foreach (MXDInfo r in MXDs)
                {
                    if (remove && !r.GetEnable()) continue;
                    Node n = new Node();
                    n.Name = r.ID;
                    n.Text = r.Name;
                    n.Tag = r;
                    n.CheckBoxVisible = true;
                    n.Checked = r.GetEnable();
                    n.DragDropEnabled = false;
                    this.Nodes.Add(n);
                }
                this.ExpandAll();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.ToString());
            }
        }

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parentnode"></param>
        public void AddNode(MXDInfo data, Node parentnode)
        {
            Node n = new Node();
            n.Name = data.ID;
            n.Text = data.Name;
            n.Tag = data;
            n.CheckBoxVisible = true;
            n.Checked = data.GetEnable();
            n.DragDropEnabled = false;
            if (parentnode == null)
                this.Nodes.Add(n);
            else
                parentnode.Nodes.Add(n);
        }

        /// <summary>
        /// 设置角色的模板的启用状态
        /// </summary>
        /// <param name="rels"></param>
        /// <param name="nodes"></param>
        public void SetEnableAll(List<Role_Rel> rels, NodeCollection nodes)
        {
            foreach (Node nd in nodes)
            {
                if (nd.Nodes.Count > 0) SetEnableAll(rels, nd.Nodes);
                if (nd.Tag == null) continue;
                string id = ((MXDInfo)nd.Tag).ID;
                Role_Rel r = null;
                if (rels != null)
                    r = rels.FirstOrDefault<Role_Rel>(t => t.RelID.Equals(id, StringComparison.CurrentCultureIgnoreCase));
                nd.Checked = r == null ? false : true;
            }
        }

        /// <summary>
        /// 设置所有模板的启用状态
        /// </summary>
        /// <param name="enable">是否启用</param>
        public void SetEnableAll(bool enable)
        {
            SetEnableAll(enable, this.Nodes);
        }

        /// <summary>
        /// 设置指定模板的启用状态
        /// </summary>
        /// <param name="enable">是否启用</param>
        /// <param name="node">需要设置的节点</param>
        public void SetEnableAll(bool enable, Node node)
        {
            if (node == null)
            {
                SetEnableAll(enable, this.Nodes);
            }
            else
            {
                node.Checked = enable;
                SetEnableAll(enable, node.Nodes);
            }
        }

        /// <summary>
        /// 设置指定模板的启用状态
        /// </summary>
        /// <param name="enable">是否启用</param>
        /// <param name="nodes">需要设置的节点集合</param>
        public void SetEnableAll(bool enable, NodeCollection nodes)
        {
            if (nodes == null || nodes.Count == 0) return;
            foreach (Node nd in nodes)
            {
                SetEnableAll(enable, nd.Nodes);
                nd.Checked = enable;
            }
        }

    }
}
